package com.tianhui.consignortwofei.mvp.ui.activity.hetong.other.view;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fgs.common.utils.DateUtil;
import com.fgs.common.utils.ToastUtil;
import com.fgs.common.widget.SelectDialog;
import com.fgs.common.widget.itemView.ClickItemView;
import com.fgs.common.widget.itemView.InputItemView;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.mvp.model.hetong.ContractInfo;
import com.tianhui.consignortwofei.mvp.ui.activity.hetong.other.DianJiaDepositContract;
import com.tianhui.consignortwofei.mvp.ui.activity.hetong.other.IContractType;
import com.tianhui.consignortwofei.mvp.ui.activity.hetong.other.IContractTypeView;

import com.xuexiang.xui.widget.edittext.MultiLineEditText;
import com.xuexiang.xui.widget.picker.widget.TimePickerView;
import com.xuexiang.xui.widget.picker.widget.builder.TimePickerBuilder;
import com.xuexiang.xui.widget.picker.widget.configure.TimePickerType;
import com.xuexiang.xui.widget.picker.widget.listener.OnTimeSelectListener;
import com.xuexiang.xutil.system.KeyboardUtils;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

import butterknife.OnClick;

/**
 * @author fgs
 * @date 2021/7/20
 * @description
 **/
public class CashDeliveryView extends LinearLayout implements IContractTypeView {
    private IContractType iContractType;
    private View contentView;
    protected LinearLayout mDepositMethodLinearLayout;
    protected ClickItemView mDeliverySettlementDateClickItemView;
    protected InputItemView mPercentageInputItemView;
    protected InputItemView mAmountInputItemView;
    protected LinearLayout ll_fukuan_yueding;

    protected MultiLineEditText mDeliverySettlementEditText;
    protected TextView mDeliverySettlementEditTextView;

    private String deliverySettlementYear = "";
    private String deliverySettlementMonth = "";
    private String deliverySettlementDay = "";
    private String deliverySettlementPercentage = "";
    private String deliverySettlementAmount = "";

    private TimePickerView mDeliverySettlementTimePickerView;

    private boolean isDeliverySettlementEditable;
    private boolean isfukuanSettlementEditable;
    private ClickItemView civ_leixing;
    private MultiLineEditText mlinet_fukuanyueding;
    private TextView tv_fukaun_yueding;

    //预付款的支付
    private boolean isYuFuEditable;
    protected LinearLayout ll_yufu_body;
    private MultiLineEditText mlinet_yufu_text;
    private TextView tv_yufu_yueding;

    public CashDeliveryView(Context context, IContractType iContractType) {
        super(context);
        this.iContractType = iContractType;
        initView();
    }

    private void initView() {
        contentView = LayoutInflater.from(getContext()).inflate(R.layout.layout_cash_delivery_view, null);


        mDepositMethodLinearLayout = contentView.findViewById(R.id.layout_cash_delivery_view_depositMethodLinearLayout);
        mDeliverySettlementDateClickItemView = contentView.findViewById(R.id.layout_cash_delivery_view_deliverySettlementDateClickItemView);
        mPercentageInputItemView = contentView.findViewById(R.id.layout_cash_delivery_view_percentageInputItemView);
        mAmountInputItemView = contentView.findViewById(R.id.layout_cash_delivery_view_amountInputItemView);

        mDeliverySettlementEditText = contentView.findViewById(R.id.layout_cash_delivery_view_deliverySettlementEditText);
        mDeliverySettlementEditTextView = contentView.findViewById(R.id.layout_cash_delivery_view_deliverySettlementEditTextView);
        //付款约定类型
        ll_fukuan_yueding = contentView.findViewById(R.id.ll_fukuan_yueding);
        civ_leixing = contentView.findViewById(R.id.civ_leixing);
        mlinet_fukuanyueding = contentView.findViewById(R.id.mlinet_fukuanyueding);
        tv_fukaun_yueding = contentView.findViewById(R.id.tv_fukaun_yueding);

        ll_yufu_body = contentView.findViewById(R.id.ll_yufu_body);
        mlinet_yufu_text = contentView.findViewById(R.id.mlinet_yufu_text);
        tv_yufu_yueding = contentView.findViewById(R.id.tv_yufu_yueding);



        mlinet_fukuanyueding.getEditText().setEnabled(false);
        mlinet_yufu_text.getEditText().setEnabled(false);

        mDeliverySettlementEditText.getEditText().setEnabled(false);
        mDeliverySettlementEditText.setContentText(iContractType.createPayWayContent(" ", " ", " ", " ", " "));

        civ_leixing.setContent("货到当天京东授信付清全款，免息授信期限 天。");
        mlinet_yufu_text.setContentText(DianJiaDepositContract.YUFU);


        initListener();
    }
    public void showDepositMethodView(){

    }

    private void initListener() {
        civ_leixing.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                fuKaunDialog();
            }
        });

        mPercentageInputItemView.setOnInputItemListener(new InputItemView.OnInputItemListener() {
            @Override
            public void onInput(String string) {
                deliverySettlementPercentage = string;
                createDeliverySettlementTip();
            }
        });

        mAmountInputItemView.setOnInputItemListener(new InputItemView.OnInputItemListener() {
            @Override
            public void onInput(String string) {
                deliverySettlementAmount = string;
                createDeliverySettlementTip();
            }
        });
        mDeliverySettlementDateClickItemView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                selectDeliverySettlementDate();
            }
        });
        mDeliverySettlementEditTextView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                deliverySettlementEdit();
            }
        });


        tv_fukaun_yueding.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                fuKuanSettlementEdit();
            }
        });

        tv_yufu_yueding.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                yuFuSettlementEdit();
            }
        });



    }

    private void createDeliverySettlementTip() {
        String deliverySettlementTip = iContractType.createPayWayContent(deliverySettlementYear, deliverySettlementMonth, deliverySettlementDay, deliverySettlementPercentage, deliverySettlementAmount);
        mDeliverySettlementEditText.setContentText(deliverySettlementTip);
    }

    public void selectDeliverySettlementDate() {
        hideKeyboard();
        if (mDeliverySettlementTimePickerView == null) {

            mDeliverySettlementTimePickerView = new TimePickerBuilder(getContext(), new OnTimeSelectListener() {
                @Override
                public void onTimeSelected(Date date, View v) {
                    String time = DateUtil.getStandardTime(date.getTime(), "yyyy-MM-dd");
                    mDeliverySettlementDateClickItemView.setContent(time);

                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(date);

                    int year = calendar.get(Calendar.YEAR);
                    int month = calendar.get(Calendar.MONTH) + 1;
                    int day = calendar.get(Calendar.DAY_OF_MONTH);

                    deliverySettlementYear = year + "";
                    deliverySettlementMonth = DateUtil.addZero(month);
                    deliverySettlementDay = DateUtil.addZero(day);

                    createDeliverySettlementTip();

                }
            })
                    .setType(TimePickerType.DEFAULT)
                    .setTitleText("交货与结算货款日期")
                    .build();
        }
        mDeliverySettlementTimePickerView.show();
    }

    private void hideKeyboard() {
        try {
            KeyboardUtils.hideSoftInput((Activity) getContext());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    protected void showKeyboard(View view) {

        try {
            KeyboardUtils.showSoftInput(view);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnClick(R.id.layout_cash_delivery_view_deliverySettlementEditTextView)
    public void deliverySettlementEdit() {
        isDeliverySettlementEditable = !isDeliverySettlementEditable;
        mDeliverySettlementEditText.getEditText().setEnabled(isDeliverySettlementEditable);
        if (isDeliverySettlementEditable) {
            mDeliverySettlementEditTextView.setText("保存");
            showKeyboard(mDeliverySettlementEditText.getEditText());
            String content = mDeliverySettlementEditText.getContentText();
            if (!TextUtils.isEmpty(content)) {
                mDeliverySettlementEditText.getEditText().setSelection(content.length());
            }
        } else {
            hideKeyboard();
            mDeliverySettlementEditTextView.setText("编辑");
        }


    }


    @OnClick(R.id.tv_fukaun_yueding)
    public void fuKuanSettlementEdit() {
        isfukuanSettlementEditable = !isfukuanSettlementEditable;
        mlinet_fukuanyueding.getEditText().setEnabled(isfukuanSettlementEditable);
        if (isfukuanSettlementEditable) {
            tv_fukaun_yueding.setText("保存");
            showKeyboard(mlinet_fukuanyueding.getEditText());
            String content = mlinet_fukuanyueding.getContentText();
            if (!TextUtils.isEmpty(content)) {
                mlinet_fukuanyueding.getEditText().setSelection(content.length());
            }
        } else {
            hideKeyboard();
            tv_fukaun_yueding.setText("编辑");
        }


    }

    @OnClick(R.id.tv_yufu_yueding)
    public void yuFuSettlementEdit() {
        isYuFuEditable = !isYuFuEditable;
        mlinet_yufu_text.getEditText().setEnabled(isYuFuEditable);
        if (isYuFuEditable) {
            tv_yufu_yueding.setText("保存");
            showKeyboard(mlinet_yufu_text.getEditText());
            String content = mlinet_yufu_text.getContentText();
            if (!TextUtils.isEmpty(content)) {
                mlinet_yufu_text.getEditText().setSelection(content.length());
            }
        } else {
            hideKeyboard();
            tv_yufu_yueding.setText("编辑");
        }


    }


    @Override
    public ContractInfo getContractViewInfo(ContractInfo contractInfo) {

        String deliverySettlement = mDeliverySettlementEditText.getContentText();
        contractInfo.payway = deliverySettlement;

        String contractContent = iContractType.getContractContent();
        contractInfo.content = contractContent;

        String paywaychoosecontent = mlinet_fukuanyueding.getContentText();
//        String paywaychoosecontent = civ_leixing.getContent();
        contractInfo.paywaychoosecontent = paywaychoosecontent;
        contractInfo.paywaychoose="1";

        String yuZhiFu = mlinet_yufu_text.getContentText();
        contractInfo.djpayment=yuZhiFu;

        return contractInfo;
    }

    @Override
    public View getContractView() {
        return contentView;
    }

    @Override
    public void initViewData(ContractInfo contractInfo) {
        mDeliverySettlementEditText.setContentText(contractInfo.payway);
    }

    protected void showToast(String message) {
        ToastUtil.showShortToast(message);
    }



    private SelectDialog mContractStandardTypeSelectDialog;
    private void fuKaunDialog() {
        if (mContractStandardTypeSelectDialog == null) {
            mContractStandardTypeSelectDialog = new SelectDialog<String>(getContext());
            mContractStandardTypeSelectDialog.setTitle("付款约定类型");
            mContractStandardTypeSelectDialog.setList(Arrays.asList(getResources().getStringArray(R.array.array_contract_pay_type)));
            mContractStandardTypeSelectDialog.setOnSelectItemClickLister(new SelectDialog.OnSelectItemClickListener<String>() {
                @Override
                public void onItemClick(String itemContent, int position) {
                    civ_leixing.setContent(itemContent);
//                    mContractInfo.qualitystandard = itemContent;
                }
            });
        }
        mContractStandardTypeSelectDialog.show();
    }

}
